package com.asm.server.handler;

import com.asm.message.PingMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class IdleStateEventHandler extends ChannelInboundHandlerAdapter {
    private int readIdleTimes = 0;
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            // 是否 读超时
            if (event.state() == IdleState.WRITER_IDLE) {
                log.debug(" 0秒 没写数据了 ，发送心跳包 ===================");
                //ctx.writeAndFlush(new PingMessage());
            } else if(event.state() == IdleState.READER_IDLE) {
                log.debug(" 12秒 没收到客户端数据了，踢掉 ===================");
                //ctx.channel().close();
            } else if(event.state() == IdleState.ALL_IDLE) {
                log.debug(" 0秒 没收到&写数据了 ===================");
            }
        }else {
            super.userEventTriggered(ctx, evt);
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        super.channelRead(ctx, msg);
    }
}
